//
//  PLPlantViewController.m
//  Plant
//
//  Created by 王飞 on 2017/9/29.
//  Copyright © 2017年 KangZhiQiang. All rights reserved.
//

#import "PLPlantViewController.h"

// 注释 需要多一些   模块化 开发  填词 开发
@interface PLPlantViewController ()

@end

@implementation PLPlantViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //初始化数组
    _dijiArr = [[NSMutableArray alloc]init];
    _boomArr = [[NSMutableArray alloc]init];
    _zidanArr = [[NSMutableArray alloc]init];
    
    //背景平移
    _bg1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, moveBg, 320, 480)];
    _bg1.image = [UIImage imageNamed:@"bg.png"];
    [self.view addSubview:_bg1];
    _bg2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, moveBg-480, 320, 480)];
    _bg2.image = [UIImage imageNamed:@"bg.png"];
    [self.view addSubview:_bg2];
    
    //创建敌机
    for (int i = 0; i<10; i++) {
        UIImageView *diji = [[UIImageView alloc]init];
        diji.frame = CGRectMake(10, i*25, 20, 20);
        diji.image = [UIImage imageNamed:@"diji.png"];
        diji.tag = 5;//5为非激活状态,6为激活状态
        [self.view addSubview:diji];
        [_dijiArr addObject:diji];
    }
    
    //创建子弹
    for (int i = 0; i<20; i++) {
        UIImageView *zidan = [[UIImageView alloc]init];
        //zidan.frame = CGRectMake(160, 450, 6, 12);
        zidan.image = [UIImage imageNamed:@"zidan.png"];
        zidan.tag = 5;//5为非激活状态,6为激活状态
        [self.view addSubview:zidan];
        [_zidanArr addObject:zidan];
    }
    
    //创建战机
    _zhanji= [[UIImageView alloc]initWithFrame:CGRectMake(160-25, 450-30, 50, 60)];
    _zhanji.animationImages = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"plane1.png"],[UIImage imageNamed:@"plane2.png"], nil];
    _zhanji.userInteractionEnabled = YES;
    _zhanji.animationDuration = 0.2;
    [_zhanji startAnimating];
    [self.view addSubview:_zhanji];
    
    //创建爆炸
    for (int i = 0 ; i<25; i++) {
        UIImageView *imgView = [[UIImageView alloc]init];
        imgView.animationImages = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"bz1.png"],[UIImage imageNamed:@"bz2.png"],[UIImage imageNamed:@"bz3.png"],[UIImage imageNamed:@"bz4.png"],[UIImage imageNamed:@"bz5.png"], nil];
        imgView.animationDuration = .5;
        imgView.animationRepeatCount = 1;
        [_boomArr addObject:imgView];
    }
    
    [NSTimer scheduledTimerWithTimeInterval:.05 target:self selector:@selector(upDate) userInfo:nil repeats:YES];
//    [self.window makeKeyAndVisible];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    orMove =NO;
    if (_zhanji ==[touch view]) {
        _location = [touch locationInView:_zhanji];
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    if (_zhanji == [touch view]) {
        CGPoint point = [touch locationInView:_zhanji];
        float dx = point.x-_location.x;
        float dy = point.y-_location.y;
        CGPoint center = _zhanji.center;
        center.x+=dx;
        center.y+=dy;
        _zhanji.center = center;
        orMove =YES;
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    if ([touch view]==_zhanji) {
        if (!orMove) {
            [self boom];
        }
    }
    
}
//全部飞机爆炸,即单击飞机全屏爆炸
- (void)boom{
    for (UIImageView *imgView in _dijiArr) {
        for (UIImageView *imgView1 in _boomArr) {
            if (![imgView1 isAnimating]) {
                imgView1.frame = imgView.frame;
                imgView.frame = CGRectZero;
                imgView.tag = 5;
                [self.view addSubview:imgView1];
                [imgView1 startAnimating];
                break;
            }
        }
    }
    orMove =NO;
}
int speed = 0;
int moveBg = 0;
bool orMove=NO;
//背景移动
- (void)bgMove{
    moveBg+=5;
    if (moveBg == 475) {
        moveBg = 0;
    }
    _bg1.frame = CGRectMake(0, moveBg, 320, 480);
    _bg2.frame = CGRectMake(0, moveBg-480, 320, 480);
}

- (void)upDate{
    
    if (speed%8 == 0) {
        [self finDiji];
        [self findZidan];
    }
    speed++;
    [self bgMove];
    [self moveDiji];
    [self moveZidan];
    [self jiZhong];
}
//判断子弹是否击中敌机
- (void)jiZhong{
    for (UIImageView *imgView in _zidanArr) {
        if (imgView.tag == 6) {
            for (UIImageView *imgView1 in _dijiArr) {
                if (imgView1.tag == 6) {
                    if (CGRectIntersectsRect(imgView.frame, imgView1.frame)) {
                        NSLog(@"000");
                        for (UIImageView *imgView2 in _boomArr) {
                            if (![imgView2 isAnimating]) {
                                imgView2.frame = imgView1.frame;
                                imgView1.frame = CGRectZero;
                                imgView1.tag = 5;
                                imgView.frame = CGRectZero;
                                imgView.tag = 5;
                                [self.view addSubview:imgView2];
                                [imgView2 startAnimating];
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
}
//激活敌机
- (void)finDiji{
    for (UIImageView *imgView in _dijiArr) {
        if (imgView.tag == 5) {
            imgView.tag =6;
            imgView.frame = CGRectMake(arc4random()%301, -20, 20, 20);
            break;
        }
    }
}
//敌机移动
- (void)moveDiji{
    for (UIImageView *imgView in _dijiArr) {
        if (imgView.tag ==6) {
            CGRect rect = imgView.frame;
            rect.origin.y+=5;
            if (rect.origin.y >480) {
                imgView.tag =5;
                rect.origin.y = -20;
            }
            imgView.frame = rect;
        }
        
        
    }
}

//激活子弹
- (void)findZidan{
    for (UIImageView *imgView in _zidanArr) {
        //NSLog(@"000");
        if (imgView.tag ==5) {
            imgView.tag =6;
            imgView.frame = CGRectMake(_zhanji.center.x-3,_zhanji.center.y-35, 6, 12);
            
            break;
        }
    }
}
//移动子弹
- (void)moveZidan{
    for (UIImageView *imgView in _zidanArr) {
        if (imgView.tag == 6) {
            CGRect rect = imgView.frame;
            rect.origin.y-=5;
            if (rect.origin.y <-12) {
                //rect.origin.y =_zhanji.center.y-25;
                imgView.frame = CGRectMake(_zhanji.center.x-3,_zhanji.center.y-35, 6, 12);
                imgView.tag = 5;
            }
            imgView.frame = rect;
        }
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
